/**
 * 针对promotions目录页面做适配
 * @description 目前promotions中计算器页面微信小程序路径是`pages/calculator/index`;
 * 百度小程序由于分包目录不支持嵌套，所以使用`promotions/calculator/index`路径，使用此插件
 * 对微信端适配。
 */
import { resolve } from "path"
export default function promotion(ctx) {
  if ("swan" === process.env.TARO_ENV) {
    const mode = process.env.NODE_ENV,
      sourceRoot = ctx.paths.sourcePath,
      from = resolve(sourceRoot, "pages/calculator"),
      to = resolve(sourceRoot, "promotions/calculator")
    ctx.onBuildStart(() => {
      function o() {
        ctx.helper.fs.cpSync(from, to, { recursive: !0, force: !0 })
      }
      if ((o(), "development" === mode)) {
        const w = ctx.helper.fs.watch(from, { recursive: !0 }, function () {
          o()
        })
        process.on("exit", function () {
          w.close()
        })
      }
    }),
      "production" === mode &&
        ctx.onBuildComplete(() => {
          ctx.helper.fs.existsSync(to) && ctx.helper.fs.removeSync(to)
        })
  }
}
